package com.zzx.threekingdom.view;

import com.zzx.threekingdom.card.CardList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

public class CardHeapView extends View implements CardList.CardListListener
{
    public CardList playCards = null;
    public CardList trashCards = null;
    
    private final static int DRAWCARD_WIDTH = 10;
    private final static int DRAWCARD_HEIGHT = 20;
    private final static int DRAWTEXT_WIDTH = 30;
    
    public CardHeapView(Context context)
    {
        this(context, (AttributeSet)null);
    }
    public CardHeapView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }
    public CardHeapView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }
    
    public void setCards(CardList play, CardList trash)
    {
        playCards = play;
        trashCards = trash;
        playCards.setListListener(this);
        trashCards.setListListener(this);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(DRAWCARD_WIDTH + DRAWTEXT_WIDTH, DRAWCARD_HEIGHT*2 + 10 + 20);
    }
    
    @Override
    protected void onDraw (Canvas canvas)
    {
        Paint paint = new Paint();
        
        Rect rc = new Rect(0, 0, DRAWCARD_WIDTH, DRAWCARD_HEIGHT);
        paint.setColor(Color.GREEN);
        canvas.drawRect(rc, paint);
        canvas.drawText("" + playCards.size(), rc.width() + 5, rc.bottom, paint);
        rc.offset(0, DRAWCARD_HEIGHT + 10);

        paint.setColor(Color.RED);
        canvas.drawRect(rc, paint);
        canvas.drawText("" + trashCards.size(), rc.width() + 5, rc.bottom, paint);
    }
    
    public void onCardsChanged(CardList cards)
    {
        invalidate();
    }

}
